let that;
let _app = getApp();
let _id;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    url: "",
    theam: "",
    emotion: "",
    navbar: [{
      txt: '分享',
      num: 1,
      img: "../../images/share.png",
      imgBlue: "../../images/shareBlue.png"
    }, {
      txt: '保存',
      num: 2,
      img: "../../images/save.png",
      imgBlue: "../../images/saveBlue.png"
    }],
    currentTab: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    that = this;
    if (options.id && options.id != "") {
      _id = options.id;
      // console.log(_app.host + "share?noteid=" + options.id);
      that.setData({
        url: _app.host + "share?noteid=" + options.id
      });
    }
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {
    return {
      title: "1111",
      path: "/pages/index/index",
      imageUrl: _app.host + "share?noteid=" + _id
    }
  },
  getLen: function(e) {
    _content = e.detail.value;
    var len = parseInt(_content.length);
    that.setData({
      len: len
    })
  },

  save: () => {
    wx.showNavigationBarLoading();
    wx.downloadFile({
      url: _app.host + "share?noteid=" + _id,
      success: function(res) {
        //图片保存到本地
        wx.saveImageToPhotosAlbum({
          filePath: res.tempFilePath,
          success: function(data) {
            wx.hideNavigationBarLoading();
            wx.showToast({
              title: '保存成功',
            })
          },
          fail: function(err) {
            //关闭加载动画
            wx.hideNavigationBarLoading();
            _app.log(err);
            if (err.errMsg.indexOf("fail") != -1 && err.errMsg.indexOf("auth") != -1) {
              _app.log("用户一开始拒绝了，我们想再次发起授权");

              //打开设置窗口
              wx.openSetting({
                success(settingdata) {
                  if (settingdata.authSetting['scope.writePhotosAlbum']) {
                    _app.log('获取权限成功，给出再次点击图片保存到相册的提示。')
                  } else {
                    _app.log('获取权限失败，给出不给权限就无法正常使用的提示')
                  }
                }
              })
            }
          }
        })
      },
      fail: function(err) {
        _app.log(err);
        wx.hideNavigationBarLoading();

      }
    })
  }
})